package com.zhxh.nzpos.goods;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.sell.ItemDeleteCallback;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 系统主界面
 * 
 * @author wangzt
 * 
 */
public class GoodsCategoryActivity extends BaseActivity implements
		ItemDeleteCallback {

	private ArrayList<Category> categoryItems;
	private CategoryListAdapter listAdapter;
	CategoryDao categoryDao;
	GoodsDao goodsDao;

	private int position;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_category);
		categoryDao = new CategoryDao(getApplicationContext());
		goodsDao = new GoodsDao(this);
		initViews();

	}

	private void initViews() {
		ListView listView = (ListView) findViewById(R.id.category_listview);

		// categoryItems = new ArrayList<Category>();
		initData();
		final EditText dealOcr = (EditText) findViewById(R.id.goods_add);
		ImageView addBtn = (ImageView) findViewById(R.id.goods_add_btn);
		addBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editable ocr = dealOcr.getText();
				String ocrStr = ocr.toString();

				if (ocrStr != null && ocrStr.trim().length() != 0) {

					// 需要从数据库中根据编号查找
					Category item = new Category();
					item.setId(ocrStr);
					item.setCategory(ocrStr);
					categoryItems.add(item);
					if (listAdapter != null) {
						listAdapter.notifyDataSetChanged();
					}
					categoryDao.save(item);
					ViewUtil.makeToast(GoodsCategoryActivity.this, "添加分类成功");
				} else {
					Toast.makeText(getApplicationContext(),
							getString(R.string.goods_category_input_hints),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

	
/*		ImageView cancelBtn = (ImageView) findViewById(R.id.cate_cancel_btn);

		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/

		listAdapter = new CategoryListAdapter(getLayoutInflater(),
				categoryItems, this);
		listView.setAdapter(listAdapter);

		/*
		 * Button removeBtn = (Button) findViewById(R.id.goods_remove_btn);
		 * removeBtn.setOnClickListener(new OnClickListener() { public void
		 * onClick(View v) { // TODO计算并传递参数到SettleAccountActivity //Intent
		 * intent = new Intent(); //intent.setClass(getApplicationContext(),
		 * SettleAccountActivity.class); //startActivity(intent); //finish();
		 * categoryItems.remove(1); listAdapter.notifyDataSetChanged(); } });
		 */

	}

	private void initData() {
		// categoryItems = new ArrayList<Category>();
		// DBHelper dbHelper = new DBHelper(getApplicationContext());
		// dbHelper.openDatabase();
		categoryItems = (ArrayList) categoryDao.getAll();

	}

	public void onDelete(int pos) {
		position = pos;
		new AlertDialog.Builder(this)
				.setTitle(R.string.goods_category_delete_dialog_title)
				.setMessage(R.string.goods_category_delete_dialog_content)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						
						
						Category category = categoryItems.get(position);
						
						int count = goodsDao.findByCategoryId(category.getId());
						if(count > 0) {
							ViewUtil.makeToast(GoodsCategoryActivity.this, "请先删除相关数据");
						}
						else {
							categoryDao.delete(category.getId());
							categoryItems.remove(position);
							listAdapter.notifyDataSetChanged();
						}
						
						// 确定按钮事件
						// setResult(RESULT_OK);
						dialog.dismiss();
						// finish();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// 取消按钮事件
						dialog.dismiss();
					}
				}).show();

	}


	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}

}
